﻿using BLL;
using Common;
using Model.CommonEntities;
using Model.StrategyEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockStrategyView.StockTask
{
    public class TaskSMACross
    {
        /// <summary>
        /// 长短期均线交叉
        /// </summary>
        /// <param name="code">代码</param>
        /// <param name="exchange">市场</param>
        /// <param name="period">所要显示的周期</param>
        /// <param name="type">数据类型，日线，周线，月线</param>
        /// <param name="date">获取数据的起始时间</param>
        /// <param name="shortpara">短周期</param>
        /// <param name="longpara">长周期</param>
        /// <returns></returns>
        public static SMACrossResult GetData(string code = null, string exchange = null, int period = -1, string type = null, string date = null, int shortperiod = 5, int longperiod = 10)
        {
            string EndDate = date == null ? DateTime.Now.ToString("yyyy-MM-dd") : Convert.ToDateTime(date).ToString("yyyy-MM-dd");
            if (date == null && Convert.ToUInt32(DateTime.Now.ToString("HHmm")) < 930)
            {
                EndDate = date == null ? DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") : Convert.ToDateTime(date).ToString("yyyy-MM-dd");
            }
            string BeginDate = Convert.ToDateTime(EndDate).AddDays(-1450).ToString("yyyy-MM-dd");
            code = code == null ? "300083" : code;
            type = type == null ? "1d" : type;
            period = period == -1 ? 30 : period;
            exchange = exchange == null ? ".XSHG" : exchange;

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("code", code + exchange.ToUpper());
            dic.Add("unit", type);
            dic.Add("date", BeginDate);
            dic.Add("end_date", EndDate);
            dic.Add("fq_ref_date", EndDate);
            dic.Add("JQUserName", "13052089963");
            dic.Add("JQPassWord", "yangyanan");
            SortedList<string, SingleStockStru> stocklist = GetJQData.get_price_period(dic);

            //长期均线
            List<decimal> MALong = new List<decimal>();
            //短期均线
            List<decimal> MAShort = new List<decimal>();
            //时间列表
            List<decimal> TList = new List<decimal>();
            //收盘价列表
            List<decimal> CloseList = new List<decimal>();
            //开盘价列表
            List<decimal> OpenList = new List<decimal>();
            //最高价列表
            List<decimal> HighList = new List<decimal>();
            //最低价列表
            List<decimal> LowList = new List<decimal>();

            foreach (var item in stocklist)
            {
                TList.Add(Convert.ToDecimal(item.Key));
                CloseList.Add(item.Value.close);
                OpenList.Add(item.Value.open);
                HighList.Add(item.Value.high);
                LowList.Add(item.Value.low);
            }


            //获取短期均线
            MAShort = AnalysisEngine.SMA(InterceptData.CutList(CloseList, period, shortperiod - 1), shortperiod).Values;
            //获取长期均线
            MALong = AnalysisEngine.SMA(InterceptData.CutList(CloseList, period, longperiod - 1), longperiod).Values;

            TList = InterceptData.CutList(TList, period);
            CloseList = InterceptData.CutList(CloseList, period);
            
            var result = new SMACrossResult
            {
                StockCode = code,
                ShortList = MAShort,
                LongList = MALong,
                TList = TList,
                CloseList = CloseList,
                BenchmarkList = CloseList
            };

            return result;
        }

        
    }
}
